package com.songcha.sakura.mvvm.home.schedule

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.bean.ComicScheduleListBean
import com.songcha.sakura.config.BaseConfig

class HomeScheduleViewModel(app: Application) : BaseViewModel<HomeScheduleRepository>(app) {

    //private val _recommendList = MutableLiveData<MutableList<SkuBean>>()
    //val recommendList: MutableLiveData<MutableList<SkuBean>> = _recommendList

    private val _comicList =  MutableLiveData<ComicScheduleListBean>()
    val comicList: MutableLiveData<ComicScheduleListBean> = _comicList
    //var list:MutableList<ComicListBean.ComicBean> = mutableListOf()

    fun getScheduleList(){
        val scheduleCacheTime= SharedPreferenceHelper.getInt("home_cache","schedule_cache_time")
        if((System.currentTimeMillis()/1000)-scheduleCacheTime<BaseConfig.HOME_CACHE_UPDATE_TIME){
            handleDataObserver((repository!! as HomeScheduleRepository).getScheduleListByDB(),object :
                IResult<ComicScheduleListBean> {
                override fun onSuccess(data: ComicScheduleListBean) {
                    if(data.data.size==0){
                        onLoadNoData()
                    }else{
                        onLoadSuccess()
                    }
                    _comicList.value=data
                }
            },true)

        }else{
            handleApiDataObserver((repository!!).getScheduleList(),object :
                IApiResult<ComicScheduleListBean> {
               override fun onSuccess(bean: ComicScheduleListBean) {
                   if (bean.data.size==0){
                       onLoadNoData()
                   }else{
                       onLoadSuccess()
                   }
                   _comicList.value=bean
                   (repository!! as HomeScheduleRepository).updateScheduleList(bean)
               }

           },true)
        }


        }

    }








